﻿//-----------------------------------------------------------------------
// <copyright file="SettingsBase.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Settings
{
    using System;
    using System.Reflection;
    using System.Runtime.CompilerServices;

    /// <summary>
    /// Base settings class object to use.
    /// </summary>
    public abstract class SettingsBase : ISettings
    {
        #region Constants

        /// <summary>
        /// Default code version to use.
        /// </summary>
        private const int DefaultCodeVersion = 1;

        /// <summary>
        /// Default version to use.
        /// </summary>
        private const int DefaultVersion = 1;

        #endregion

        #region Fields - Private

        /// <summary>
        /// Version of the settings object.
        /// </summary>
        private int version = DefaultVersion;

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor.
        /// </summary>
        public SettingsBase()
        {
            this.ApplyDefaultValues();
        }

        #endregion

        #region Properties - Public

        /// <summary>
        /// Gets the current code version of the settings object.
        /// </summary>
        public virtual int CodeVersion
        {
            get
            {
                return DefaultCodeVersion;
            }
        }

        /// <summary>
        /// Gets or sets the current version of the settings object.
        /// </summary>
        public virtual int Version
        {
            get
            {
                return this.version;
            }

            set
            {
                this.version = value;
            }
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Reset the settings object to default values.
        /// </summary>
        public void Reset()
        {
            this.ApplyDefaultValues();
            this.InternalReset();
        }

        /// <summary>
        /// Upgrade the settings object from an old version to a new.
        /// </summary>
        /// <param name="fromVersion">Version we are upgrading from.</param>
        /// <param name="toVersion">Version we are upgrading to.</param>
        public virtual void Upgrade(int fromVersion, int toVersion)
        {
        }

        #endregion

        #region Methods - Protected

        /// <summary>
        /// Method called by Reset() to reset the object.
        /// </summary>
        protected virtual void InternalReset()
        {
        }

        /// <summary>
        /// Clamp the passed value to the range for the property named propertyName
        /// if it has a default value attribute on it.
        /// </summary>
        /// <typeparam name="T">Value type to clamp.</typeparam>
        /// <param name="value">Value to clamp.</param>
        /// <param name="propertyName">Property name to look for.</param>
        /// <returns>Returns the clamped value.</returns>
        protected T Clamp<T>(T value, [CallerMemberName] string propertyName = "unknown")
        {
            Type type = GetType();
            PropertyInfo property = type.GetProperty(propertyName);

            if (property == null)
            {
                return value;
            }

            object clampedValue = value;
            SettingsDefaultValueAttribute defaultAttrib = property.GetCustomAttribute<SettingsDefaultValueAttribute>(false);
            if (defaultAttrib != null)
            {
                clampedValue = defaultAttrib.Clamp(value);
            }

            return (T)clampedValue;
        }

        #endregion

        #region Methods - Private

        /// <summary>
        /// Apply default values to the class.
        /// </summary>
        private void ApplyDefaultValues()
        {
            Type type = GetType();
            PropertyInfo[] properties = type.GetProperties();

            foreach (PropertyInfo property in properties)
            {
                SettingsDefaultValueAttribute defaultAttrib = property.GetCustomAttribute<SettingsDefaultValueAttribute>(false);
                if (defaultAttrib == null)
                {
                    continue;
                }

                property.SetValue(this, defaultAttrib.DefaultValue);
            }
        }

        #endregion
    }
}
